﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BUL;
using Common;
using Entity;

namespace PM.Admin.ctrUser
{
    public partial class ctrNews : System.Web.UI.UserControl
    {
        readonly NewsBul bul = new NewsBul();
        readonly GroupNewsBul groupNewsBul=new GroupNewsBul();
        private static string Id = "";
        private static string Lang = "vi";

        private void LoadData()
        {
            GridView1.DataSource = bul.News_GetByAll("vi");
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                LoadGroupNewsList();
                ViewState["flag"] = "add";
            }
        }
        protected void LoadGroupNewsList()
        {
            ddlGroupNews.Items.Clear();
            ddlGroupNews.DataTextField = "Name";
            ddlGroupNews.DataValueField = "Id";
            ddlGroupNews.DataSource = groupNewsBul.GroupNews_GetByAll("vi");
            ddlGroupNews.DataBind();
        }
        protected void ChkAll(object sender, EventArgs e)
        {
            var chkh = (CheckBox)GridView1.HeaderRow.FindControl("cbAll");
            foreach (GridViewRow row in GridView1.Rows)
            {
                var chk = (CheckBox)row.FindControl("cbitem");
                chk.Checked = chkh.Checked;
            }
        }

        

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            LoadData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            LoadData();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (GridView1.DataKeys[e.RowIndex] == null)
                return;
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            bul.News_Delete(id);
            LoadData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[6].Attributes.Add("Onclick", "return confirm('Bạn xóa dữ liệu này?')");
            }
        }

        
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string _id = Convert.ToString(e.CommandArgument);
            if (e.CommandName == "select")
            {
                Id = _id;
                List<Entity.News> list = bul.News_GetById(_id);
                txtName.Text = list[0].Name;
                txtTitle.Text = list[0].Title;
                txtKeyword.Text = list[0].Keyword;
                txtImage.Text = list[0].Image;
                txtFile.Text = list[0].File;
                txtContent.Text = list[0].Content;
                fckDetail.Value = list[0].Detail;
                txtDate.Text = DateTimeClass.ConvertDateTime(list[0].Date);
                txtDescription.Text = list[0].Description;
                cbActive.Checked = list[0].Active == "1" || list[0].Active == "true";
                cboIndex.Checked = list[0].Index == "1" || list[0].Index == "true";
                cboOrder.Checked = list[0].Order == "1" || list[0].Order == "true";
                cboPriority.Checked = list[0].Priority == "1" || list[0].Priority == "true";
                cboSlide.Checked = list[0].Check1 == "1" || list[0].Check1 == "true";
                cbo2.Checked = list[0].Check2 == "1" || list[0].Check2 == "true";
                cbo3.Checked = list[0].Check3 == "1" || list[0].Check3 == "true";
                cbo4.Checked = list[0].Check4 == "1" || list[0].Check4 == "true";
                cbo5.Checked = list[0].Check5 == "1" || list[0].Check5 == "true";
                cbo6.Checked = list[0].Check6 == "1" || list[0].Check6 == "true";
                ddlGroupNews.SelectedValue = list[0].GroupNewsId;
            }
            ViewState["flag"] = "edit";

        }
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            PageHelper.ResetControlValues(this);
            LoadGroupNewsList();
            ViewState["flag"] = "add";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Entity.News obj = new Entity.News();
                obj.Id = Id;
                obj.Index = cboIndex.Checked?"1":"0";
                obj.Name = txtName.Text;
                obj.Image = txtImage.Text;
                obj.File = txtFile.Text;
                obj.Date = DateTimeClass.ConvertDateTime(txtDate.Text, "dd/MM/yyyy HH:mm:ss tt");
                obj.Description = txtDescription.Text;
                obj.Active = cbActive.Checked ? "1" : "0";
                obj.Lang = Lang;
                obj.Title = txtTitle.Text;
                obj.Keyword = txtKeyword.Text;
                obj.Order = cboOrder.Checked ? "1" : "0";
                obj.Check1 = cboSlide.Checked ? "1" : "0";
                obj.Check2 = cbo2.Checked ? "1" : "0";
                obj.Check3 = cbo3.Checked ? "1" : "0";
                obj.Check4 = cbo4.Checked ? "1" : "0";
                obj.Check5 = cbo5.Checked ? "1" : "0";
                obj.Check6 = cbo6.Checked ? "1" : "0";
                obj.Priority = cboPriority.Checked ? "1" : "0";
                obj.Content = txtContent.Text;
                obj.Detail = fckDetail.Value;
                obj.GroupNewsId = ddlGroupNews.SelectedValue;
                obj.Tag = StringClass.NameToTag(txtName.Text);
                if (ViewState["flag"].Equals("add"))
                {
                    bul.News_Insert(obj);
                }
                else if (ViewState["flag"].Equals("edit"))
                {
                    bul.News_Update(obj);
                }
                ViewState["flag"] = "add";
                LoadData();
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            int sodong = GridView1.Rows.Count;
            for (int i=sodong-1; i >-1; i--)
            {
                var id = GridView1.DataKeys[i].Value.ToString();;
                var ck =(CheckBox) GridView1.Rows[i].FindControl("cbitem");
                        if (ck.Checked)
                        {
                            bul.News_Delete(id);
                            LoadData();
                        }
                }
            }

        protected void btbSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                return;
            }
            string name = txtSearch.Text;
            GridView1.DataSource = bul.News_Search(name, name, name, name);
            GridView1.DataBind();
        }
        }

}